<template>
  <view>
   <view class="header">
     <view class="back" @click="back()">
       <uni-icons type="left" size="20"></uni-icons>
     </view>
     <text>商品详情</text>
   </view>
    <view class="goods-Info">
      <image :src="goodsDetail.mainImageUrl" mode="widthFix" @click="preview2(index)"></image>
      <view class="textInfo">
        <view class="goods-title">
          <view class="goods-type" :style='{backgroundColor: typeColor[goodsDetail.categoryName]}'>
            {{goodsDetail.categoryName}}
          </view>
          <text>{{goodsDetail.name}}</text>
        </view>
        <view class="price">
          <text class="value">{{goodsDetail.price}}</text>
          <text class='name'>积分</text>
          <text class="add">+</text>
          <text class="value">{{goodsDetail.cashPrice}}</text>
          <text class="name">元</text>
        </view>
      </view>
    </view>
    <view class="address-box" @click="goChooseAddress()">
      <uni-icons type='location' size="28"></uni-icons>
      <view class="addressInfo" v-if="addressInfo">
        <view class="mainInfo">
          {{addressInfo.recipientName}} {{addressInfo.phoneNumber}}
        </view>
        <view class="addressDetail">
          {{addressInfo.privince + addressInfo.city + addressInfo.county + addressInfo.detailAddress}}
        </view>
      </view>
      <view class="default" v-else>
        请添加收货地址
      </view>
      <uni-icons type="right" size="16" color="#ddd"></uni-icons>
    </view>
    <view class="goods-detail">
      <view class="detail-title">
        商品简介
      </view>
      <view class="detail">
        <view class="goods-title">
          商品名称：{{goodsDetail.name}}
        </view>
        <view class="goods-description">
          商品描述：{{goodsDetail.description}}
        </view>
        <view class="goods-detailsInfo">
          商品信息：{{goodsDetail.details}}
        </view>
      </view>
    </view>
    <view class="goods-images" v-if='goodsDetail.images'>
      <view class="list-title">
        商品详情
      </view>
      <view class="image-container">
        <block v-for="(item,index) in goodsDetail.images" :key="index">
          <image :src="item" @click="preview(index)" mode="widthFix"></image>
        </block>
      </view>
    </view>
    <view class="footer">
      <view class="buy" @click="open()">
        立即购买
      </view>
    </view>
    <uni-popup ref="popup" type="bottom" mask-background-color='rgba(0,0,0,0.9)'>
      <buy-goods :addressInfo='addressInfo' :goodsCount='goodsCount' :goodsDetail='goodsDetail' @closePopup='closePopup'
        @changeCount='changeCount'></buy-goods>
    </uni-popup>
  </view>
</template>

<script>
  import {
    mapState
  } from 'vuex'
  export default {
    data() {
      return {
        goodsId: 0,
        addressInfo: {},
        goodsCount: 1,
        goodsDetail: {
          colorVOS: []
        },
        typeColor: {
          '服饰类': 'orange',
          '电子产品类': '#0020ff',
          '书籍类': '#f00',
          '生活用品类': '#6e89eb',
          '食品类': 'yellowgreen',
          '其他类': 'purple',
        }
      };
    },
    created() {
      this.getAddressInfo()
    },
    onLoad(options) {
      this.goodsId = options.goodsId
      this.getGoodsDetail(this.goodsId)
    },
    onShow() {
      let that = this
      uni.$on('addressInfo', function(data) {
        that.addressInfo = JSON.parse(data)
      })
    },
    methods: {
      back() {
        uni.navigateBack()
      },
      async getAddressInfo() {
        const {
          data: {
            data
          }
        } = await uni.$http.get(`/users/address/getDefault/${this.userInfo.studentId}`)
        console.log(data);
        this.addressInfo = data
      },
      open() {
        this.$refs.popup.open('bottom')
      },
      closePopup() {
        this.$refs.popup.close()
      },
      changeCount(e) {
        this.goodsCount = e
      },
      goChooseAddress() {
        uni.navigateTo({
          url: '/subpkg/chooseAddress/chooseAddress'
        })
      },
      preview(i) {
        uni.previewImage({
          current: i,
          urls: this.goodsDetail.images
        })
      },
      preview2(){
        uni.previewImage({
          current: 1,
          urls: [this.goodsDetail.mainImageUrl]
        })
      },
      async getGoodsDetail(id) {
        const {
          data: {
            data
          }
        } = await uni.$http.get(`/users/product/${id}`)
        console.log(data);
        this.goodsDetail = data
      },
    },
    computed: {
      ...mapState('my_user', ['userInfo']),
    },
  }
</script>

<style lang="scss">
  .header {
    position: relative;
    width: 100%;
    height: 140rpx;
    text-align: center;
    font-size: 36rpx;
    font-weight: 700;
    line-height: 170rpx;
    background-color: #fff;
  
    .back {
      position: absolute;
      top: 4rpx;
      left: 23rpx;
      height: 50rpx;
    }
  }
  
  .goods-Info {
    display: flex;
    flex-direction: column;
    background-color: #fff;

    image {
      margin: 0 auto;
    }

    .textInfo {
      padding: 20rpx 20rpx;

      .goods-title {
        display: flex;
        align-items: center;

        .goods-type {
          width: fit-content;
          padding: 4rpx 12rpx;
          background-color: red;
          color: #fff;
          font-size: 26rpx;
          text-align: center;
        }

        text {
          margin-left: 20rpx;
          font-size: 46rpx;
          font-weight: 700;
        }
      }

      .price {
        margin-top: 20rpx;
        font-size: 36rpx;
        color: #ff7000;

        .value {
          margin-right: 5rpx;
          font-size: 50rpx;
          font-weight: 700;
        }

        .name {
          font-size: 34rpx;
        }
      }
    }
  }

  .address-box {
    display: flex;
    padding: 10rpx 20rpx;
    padding-bottom: 20rpx;
    border-top: 1rpx solid #eee;
    background-color: #fff;

    .addressInfo {
      margin-left: 10rpx;

      .mainInfo {
        display: flex;
        align-items: center;
        width: 600rpx;
        height: 60rpx;
        font-size: 32rpx;
        font-weight: 700;
      }

      .addressDetail {
        font-size: 28rpx;
        margin-top: 6rpx;
        color: #555;
      }
    }

    .default {
      width: 600rpx;
      font-size: 36rpx;
      font-weight: 700;
    }
  }

  .goods-detail {
    margin-top: 20rpx;
    padding-bottom: 30rpx;
    background-color: #fff;
    
    .detail-title {
      position: relative;
      width: 100%;
      height: 80rpx;
      padding: 0 60rpx;
      line-height: 80rpx;
      background-color: #fff;
      font-size: 36rpx;
    
      &::before {
        content: ' ';
        display: block;
        width: 4px;
        height: 20px;
        background-color: #b6eaae;
        position: absolute;
        left: 20rpx;
        top: 50%;
        transform: translateY(-50%);
      }
    }

    .detail {
      padding: 0 20rpx;

      .goods-title,
      .goods-description,
      .goods-detailsInfo {
        margin-top: 30rpx;
        font-size: 30rpx;
        color: #777;
      }
    }
  }

  .goods-images {
    
    .list-title {
      position: relative;
      width: 100%;
      height: 80rpx;
      margin-bottom: 10rpx;
      padding: 0 60rpx;
      line-height: 80rpx;
      background-color: #fff;
      font-size: 36rpx;
    
      &::before {
        content: ' ';
        display: block;
        width: 4px;
        height: 20px;
        background-color: #b6eaae;
        position: absolute;
        left: 20rpx;
        top: 50%;
        transform: translateY(-50%);
      }
    }

    margin-top: 20rpx;
    background-color: #fff;

    .image-container {
      display: flex;
      flex-direction: column;
      align-items: center;
      padding-bottom: 120rpx;

      image {
        margin: 0 auto;
      }

    }
  }

  .footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    height: 100rpx;
    padding: 20rpx 30rpx;
    border-top: 1rpx solid #e9e9e9;
    background-color: #fff;

    .buy {
      width: 90%;
      height: 80rpx;
      border-radius: 6rpx;
      text-align: center;
      line-height: 80rpx;
      background-color: #b6edaa;
      color: #fff;
      font-size: 34rpx;
    }
  }
</style>